<template>
  <div id="call-insurance">
    <div v-if="insuranceList && insuranceList.length > 0">
      <div v-for="(item, index) in insuranceList" :key="index" class="item">
        <div class="logo">
          <img :src="item.logo" alt="">
        </div>
        <div class="info">
          <span class="title" v-if="item.title">{{item.title}}</span>
          <span class="number" v-if="item.number">{{`电话:${item.number}`}}</span>
        </div>
        <div class="call-icon">
          <a :href="`tel:${item.number}`">
            <i class="iconfont">&#xe6f4;</i>
          </a>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// services
import {
  OneKeyInsurance,
  ListInsurance
} from "services/rescue/call.js"
// images
import default_icon from "assets/app_icon.png"

export default {
  name: "call-insurance",
  data() {
    return {
      // resources
      default_icon,
      insuranceList: []
    }
  },
  created() {
    this.getInsuranceList()
  },
  methods: {
    // 获取报险公司列表
    getInsuranceList() {
      OneKeyInsurance.bind(this)(this.getOneKeyInsuranceReq(), res => {
        this.setInsuranceList(res)
      }, (err, code) => {
        this.insuranceList = []
        this.listInsurance()
      })
    },
    // get request
    getOneKeyInsuranceReq() {
      return {
        carId: this.$.carId
      }
    },
    // OneKeyInsurance失败则展示预置的保险公司列表
    listInsurance() {
      ListInsurance.bind(this)({}, res => {
        this.setInsuranceList(res)
      })
    },
    // set list
    setInsuranceList(res) {
      let insuranceList = [];

      // 如果车辆保险公司存在，则车辆保险公司在前
      if (res.carInsuranceDatas && res.carInsuranceDatas.length > 0) {
        insuranceList = insuranceList.concat(res.carInsuranceDatas);
      }
      this.insuranceList = insuranceList.concat(res.insuranceDatas);
      this.formatInsuranceList(this.insuranceList)
    },
    // 格式化
    formatInsuranceList(item) {
      item.forEach(item => {
        item.logo = (item.logo && this.addPath(item.logo)) || this.default_icon
        item.title = item.name
        item.number = item.phoneNumber
      })
    }
  }
}
</script>

<style lang="scss" scoped>
#call-insurance {
  .item {
    display: flex;
    justify-content: space-between;
    align-items: stretch;
    position: relative;
    &:after {
      content: " ";
      position: absolute;
      left: 0;
      bottom: 0;
      width: 100%;
      height: 0;
      border-bottom: 1px solid #929292;
      transform: scaleY(0.4);
    }
    > div {
      padding: 10px;
    }
    .logo {
      img {
        width: 40px;
        height: 40px;
      }
    }
    .info {
      width: 100%;
      display: flex;
      flex-direction: column;
      color: #707070;
      span {
        line-height: 20px;
      }
      .title {
        color: #000;
        font-size: 14px;
      }
    }
    .call-icon {
      align-self: center;
      width: 60px;
      color: #707070;
      .iconfont {
        color: #707070;
        font-size: 25px;
        line-height: 40px;
      }
    }
  }
}
</style>
